package com.one.rope.mvp.web.controller.user;

import com.one.rope.mvp.basecore.annotation.LogMethod;
import com.one.rope.mvp.basecore.util.RegexUtils;
import com.one.rope.mvp.basecore.bean.ResponseObject;
import com.one.rope.mvp.web.common.dto.user.auth.UserLoginDTO;
import com.one.rope.mvp.web.common.dto.user.auth.UserLoginVO;
import com.one.rope.mvp.web.common.dto.user.userinfo.GetUserInfoVO;
import com.one.rope.mvp.web.common.dto.user.userinfo.GetVerifyCodeDTO;
import com.one.rope.mvp.web.common.dto.user.userinfo.RegisterBUserInfoDTO;
import com.one.rope.mvp.web.common.dto.user.userinfo.RegisterCUserInfoDTO;
import com.one.rope.mvp.web.common.dto.user.userinfo.SaveUserInfoDTO;
import com.one.rope.mvp.web.common.util.ObjectUtils;
import com.one.rope.mvp.web.controller.BaseController;
import com.one.rope.mvp.web.service.user.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Weijian.liu
 * @desc 小程序端-用户模块控制器
 * @date 2020/02/19
 */
@Api(tags = "B1小程序端-用户模块")
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController extends BaseController {

  @Autowired
  UserService userService;

  @LogMethod
  @ApiOperation(value = "001用户登录", notes = "小程序用户授权登录登录接口", response = UserLoginVO.class)
  @PostMapping("/auth/login")
  public ResponseObject<UserLoginVO> userLogin(@RequestBody UserLoginDTO requestDTO) {
    ObjectUtils.requestNonEmpty(requestDTO.getCode(), "code不能为空");
    ObjectUtils.requestNonNull(requestDTO.getUserType(), "用户类型不能为空");
    try {
      return userService.userLogin(requestDTO);
    } catch (Exception e) {
      log.error("/user/auth/login接口异常", e);
      return ResponseObject.systemError(e);
    }
  }

  @LogMethod
  @ApiOperation(value = "002获取用户信息", notes = "获取用户信息接口", response = UserLoginVO.class)
  @GetMapping("/api/user/getUserInfo")
  public ResponseObject<GetUserInfoVO> getUserInfo() {
    try {
      return userService.getUserInfo();
    } catch (Exception e) {
      log.error("/api/user/getUserInfo接口异常", e);
      return ResponseObject.systemError(e);
    }
  }

  @LogMethod
  @ApiOperation(value = "003获取验证码", notes = "获取短信验证码接口")
  @PostMapping("/api/getVerifyCode")
  public ResponseObject getVerifyCode(@RequestBody GetVerifyCodeDTO requestDTO){
    ObjectUtils.requestNonEmpty(requestDTO.getMobile(), "手机号不能为空");
    ObjectUtils.requestNonNull(requestDTO.getType(), "业务类型不能为空");
    ObjectUtils.requsetCondition(RegexUtils.isMobile(requestDTO.getMobile()), "请输入正确的手机号");
    ObjectUtils.requsetCondition(requestDTO.getType() == 1 || requestDTO.getType() == 2, "非法的业务类型");
    try {
      return userService.getVerifyCode(requestDTO);
    } catch (Exception e) {
      log.error("/api/getVerifyCode接口异常", e);
      return ResponseObject.systemError(e);
    }
  }

  @LogMethod
  @ApiOperation(value = "004C端用户注册", notes = "C端用户注册接口，保存C端用户信息")
  @PostMapping("/api/user/CRegister")
  public ResponseObject registerC(@RequestBody RegisterCUserInfoDTO requestDTO) {
    ObjectUtils.requestNonEmpty(requestDTO.getName(), "真实姓名不能为空");
    ObjectUtils.requestNonNull(requestDTO.getSex(), "称谓不能为空");
    ObjectUtils.requestNonEmpty(requestDTO.getCode(), "验证码不能为空");
    ObjectUtils.requestNonEmpty(requestDTO.getMobile(), "手机号不能为空");
    ObjectUtils.requsetCondition(RegexUtils.isMobile(requestDTO.getMobile()), "请输入正确的手机号");
    try {
      return userService.registerC(requestDTO);
    } catch (Exception e) {
      log.error("/api/user/CRegister接口异常", e);
      return ResponseObject.systemError(e);
    }
  }

  @LogMethod
  @ApiOperation(value = "005B端用户注册", notes = "B端用户注册接口，保存B端用户信息")
  @PostMapping("/api/user/BRegister")
  public ResponseObject registerB(@RequestBody RegisterBUserInfoDTO requestDTO) {
    ObjectUtils.requestNonEmpty(requestDTO.getName(), "真实姓名不能为空");
    ObjectUtils.requestNonNull(requestDTO.getSex(), "称谓不能为空");
    ObjectUtils.requestNonEmpty(requestDTO.getCode(), "验证码不能为空");
    ObjectUtils.requestNonEmpty(requestDTO.getMobile(), "手机号不能为空");
    ObjectUtils.requsetCondition(RegexUtils.isMobile(requestDTO.getMobile()), "请输入正确的手机号");
    ObjectUtils.requestNonEmpty(requestDTO.getBirthday(), "出生日期不能为空");
    ObjectUtils.requestNonEmpty(requestDTO.getCompanyName(), "公司名称不能为空");
    ObjectUtils.requestNonNull(requestDTO.getCompanyPost(), "任职岗位不能为空");
//    ObjectUtils.requestNonNull(requestDTO.getCompanyValue(), "公司估值不能为空");
//    ObjectUtils.requestNonNull(requestDTO.getValueCurrency(), "估值币种不能为空");
    try {
      return userService.registerB(requestDTO);
    } catch (Exception e) {
      log.error("/api/user/BRegister接口异常", e);
      return ResponseObject.systemError(e);
    }
  }

  @LogMethod
  @ApiOperation(value = "006账号管理保存用户信息", notes = "账号管理保存用户信息接口")
  @PostMapping("/api/user/saveUserInfo")
  public ResponseObject saveUserInfo(@RequestBody SaveUserInfoDTO requestDTO) {
    ObjectUtils.requestNonEmpty(requestDTO.getName(), "真实姓名不能为空");
    ObjectUtils.requestNonNull(requestDTO.getSex(), "称谓不能为空");
    try {
      return userService.saveUserInfo(requestDTO);
    } catch (Exception e) {
      log.error("/api/user/saveUserInfo接口异常", e);
      return ResponseObject.systemError(e);
    }
  }

  @LogMethod
  @ApiOperation(value = "007清除当前用户信息接口", notes = "清除当前用户信息接口(测试期间使用)")
  @GetMapping("/api/user/deleteUser")
  public ResponseObject deleteUser() {
    try {
      return userService.deleteUser();
    } catch (Exception e) {
      log.error("/api/user/deleteUser接口异常", e);
      return ResponseObject.systemError(e);
    }
  }
}
